import dayjs from 'dayjs'

const ymd = dayjs().format('YYYY-MM-DD')
export const getCurrentDate = (props) => {
  if (props.value == null || props.value === '') {
    return dayjs()
  }

  if (props.type === 'time') {
    return dayjs(`${ymd} ${props.value}`)
  }

  return dayjs(props.value)
}
export const getMinDate = (props, nowYear) => {
  if (props.minDate == null) {
    return dayjs(new Date(nowYear - 10, 0, 1, 0, 0, 0))
  }

  if (props.type === 'time') {
    return dayjs(`${ymd} ${props.minDate}`)
  }

  return dayjs(props.minDate)
}

export const getMaxDate = (props, nowYear) => {
  if (props.maxDate == null) {
    return dayjs(new Date(nowYear + 10, 11, 31, 23, 59, 59))
  }

  if (props.type === 'time') {
    return dayjs(`${ymd} ${props.maxDate}`)
  }

  return dayjs(props.maxDate)
}

interface IColumn {
  label: string
  value: string | number
  values?: IColumn[]
  children?: IColumn[]
}

export const getColumns = (columnFrom, columnTo, formatLabel) => {
  const columns: IColumn[] = []

  for (let columnIndex = columnFrom; columnIndex <= columnTo; columnIndex++) {
    columns.push({
      value: columnIndex,
      label: formatLabel(columnIndex),
    })
  }
  return columns
}

export const getYears = ({ minDate, maxDate }) => {
  return getColumns(minDate.year(), maxDate.year(), (x) => `${x}年`)
}

export const getMonths = ({ minDate, maxDate, year }) => {
  let columnFrom = 0
  let columnTo = 11

  if (minDate.year() === year) {
    columnFrom = minDate.month()
  }
  if (maxDate.year() === year) {
    columnTo = maxDate.month()
  }

  return getColumns(columnFrom, columnTo, (x) => `${x + 1}月`)
}

export const getDates = ({ minDate, maxDate, year, month }) => {
  let columnFrom = 1
  let columnTo
  if ([0, 2, 4, 6, 7, 9, 11].includes(month)) {
    columnTo = 31
  } else if (month === 1) {
    if (year % 4 === 0) {
      columnTo = 29
    } else {
      columnTo = 28
    }
  } else {
    columnTo = 30
  }

  if (minDate.year() === year && minDate.month() === month) {
    columnFrom = minDate.date()
  }
  if (maxDate.year() === year && maxDate.month() === month) {
    columnTo = maxDate.date()
  }

  return getColumns(columnFrom, columnTo, (x) => `${x}日`)
}

export const getHours = ({ minDate, maxDate, year, month, date }) => {
  let columnFrom = 0
  let columnTo = 23

  if (minDate.year() === year && minDate.month() === month && minDate.date() === date) {
    columnFrom = minDate.hour()
  }
  if (maxDate.year() === year && maxDate.month() === month && maxDate.date() === date) {
    columnTo = maxDate.hour()
  }

  return getColumns(columnFrom, columnTo, (x) => `${x}时`)
}

export const getMinutes = ({ minDate, maxDate, year, month, date, hour }) => {
  let columnFrom = 0
  let columnTo = 59

  if (minDate.year() === year && minDate.month() === month && minDate.date() === date && minDate.hour() === hour) {
    columnFrom = minDate.minute()
  }
  if (maxDate.year() === year && maxDate.month() === month && maxDate.date() === date && maxDate.hour() === hour) {
    columnTo = maxDate.minute()
  }

  return getColumns(columnFrom, columnTo, (x) => `${x}分`)
}

export const getSeconds = ({ minDate, maxDate, year, month, date, hour, minute }) => {
  let columnFrom = 0
  let columnTo = 59

  if (
    minDate.year() === year &&
    minDate.month() === month &&
    minDate.date() === date &&
    minDate.hour() === hour &&
    minDate.minute() === minute
  ) {
    columnFrom = minDate.second()
  }
  if (
    maxDate.year() === year &&
    maxDate.month() === month &&
    maxDate.date() === date &&
    maxDate.hour() === hour &&
    maxDate.minute() === minute
  ) {
    columnTo = maxDate.second()
  }

  return getColumns(columnFrom, columnTo, (x) => `${x}秒`)
}

const getColumnValue = (columns, oldVal) => {
  let newVal

  if (columns.map((x) => x.value).includes(oldVal)) {
    newVal = oldVal
  } else {
    const firstVal = columns[0].value
    const lastVal = columns[columns.length - 1].value

    if (oldVal < firstVal) {
      newVal = firstVal
    } else if (oldVal > lastVal) {
      newVal = lastVal
    }
  }

  return newVal
}

export const setMonth = (dateInfo, state, vm) => {
  const columns = getMonths(dateInfo)
  const newVal = getColumnValue(columns, state.month)

  vm.$refs.picker.setColumnValues(1, columns, newVal)

  return newVal
}

export const setDate = (dateInfo, state, vm) => {
  const columns = getDates(dateInfo)
  const newVal = getColumnValue(columns, state.date)

  vm.$refs.picker.setColumnValues(2, columns, newVal)

  return newVal
}

export const setHour = (dateInfo, state, vm) => {
  const columns = getHours(dateInfo)
  const newVal = getColumnValue(columns, state.hour)

  vm.$refs.picker.setColumnValues(3, columns, newVal)

  return newVal
}

export const setMinute = (dateInfo, state, vm, columnIndex = 4) => {
  const columns = getMinutes(dateInfo)
  const newVal = getColumnValue(columns, state.minute)

  vm.$refs.picker.setColumnValues(columnIndex, columns, newVal)

  return newVal
}

export const setSecond = (dateInfo, state, vm, columnIndex = 5) => {
  const columns = getSeconds(dateInfo)
  const newVal = getColumnValue(columns, state.second)

  vm.$refs.picker.setColumnValues(columnIndex, columns, newVal)

  return newVal
}

export const getDefaultValue = (props, innerValue) => {
  if (props.type === 'yearmonth') {
    return innerValue.slice(0, 2)
  }

  if (props.type === 'datetime') {
    return innerValue.slice(0, 5)
  }

  if (props.type === 'time') {
    return innerValue.slice(3)
  }

  return innerValue.slice(0, 3)
}
